<template>
  <n-modal
    v-model:show="modelValue.toDelete"
    :mask-closable="false"
    preset="dialog"
    title="确认"
    content="你确认"
    positive-text="确认"
    negative-text="算了"
    @positive-click="onPositiveClick"
    @negative-click="modelValue.toDelete = false"
  />
</template>

<script
  lang="ts"
  setup
  generic="T extends object &
  { toEdit: boolean,toDelete:boolean,toDealRowId: string|undefined,toRecord:boolean}"
>
import { TaskLogData } from "@model/TaskLogMap";
import UseCacheIdStatMap from "@pinias/UseCacheIdStatMap";
import UseCacheMemory from "@pinias/UseCacheMemory";
import UseCacheTaskLog from "@pinias/UseCacheTaskLog";
import UseSurfaceSetting from "@pinias/UseSurfaceSetting";
import pinia from "@pinias/pinia";
import { DateTimeStr } from "@utils/DateUtils";
import { NModal } from "naive-ui";
import { computed } from "vue";

// 全局数据
const { getCacheTaskLogMap } = UseCacheTaskLog(pinia);
const cacheTaskLogMap = getCacheTaskLogMap();
const { getCacheIdStatMap } = UseCacheIdStatMap(pinia);
const cacheIdStatMap = getCacheIdStatMap();
const { getCacheMemory } = UseCacheMemory(pinia);
const cacheMemory = getCacheMemory();
const { getSurfaceSetting } = UseSurfaceSetting(pinia);
const surfaceSetting = getSurfaceSetting();
// 父组件数据
const props = defineProps<{ modelValue: T }>();
const emit = defineEmits(["update:modelValue"]);
const modelValue = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});
// const message = useMessage();
const onPositiveClick = async () => {
  const id = modelValue.value.toDealRowId;
  if (id !== undefined) {
    const timeStamp = DateTimeStr.getTimeStamp(id);
    const taskLogMap = await cacheTaskLogMap.getTaskLogMap(timeStamp);
    let tosave = taskLogMap.taskLogMap.get(id);
    if (tosave !== undefined) {
      // 统计情况：
      const oldFatherId = await TaskLogData.getFatherId(tosave.type, tosave.taskId);
      // console.log("onPositiveClick", tosave, oldFatherId);
      await cacheIdStatMap.changeTaskLogData4Stat(
        [tosave],
        oldFatherId.map(value => value.id),
        [],
        [],
      );
      // 回忆情况
      await cacheMemory.deleteMemory([tosave]);
      tosave = await TaskLogData.getFullMediaPath(tosave);
      surfaceSetting.toRemoveMediaPath = [...tosave.mediaList, ...tosave.picList];
      console.log(surfaceSetting.toRemoveMediaPath);
    }
    // 日志情况
    await cacheTaskLogMap.deleteTaskLogData([id]);

    modelValue.value.toDelete = false;
    // message.success("");
  }
};
</script>
